﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using WorldBankBBS.Logic;
using WorldBankBBS.Pathing;

namespace WorldBankBBS.Subboards
{
    public class SubboardPathHandler : IPathHandler
    {
        public Session Session { get; private set; }
        private SubboardsEvents _events = null;

        public SubboardPathHandler(Session session)
        {
            Session = session;
            _events = BbsDispatcher.GetDispatcher(session).GetObject<SubboardsEvents>(typeof(SubboardsEvents).FullName);
        }

        public void ProcessPath(string path)
        {
            var parts = path.Split(new[] {'|'});
            Guid temp;

            switch (parts[0].ToLower())
            {
                case "listmessages":
                    _events.InvokeListMessages();
                    break;
                case "listthreads":
                    _events.InvokeListThreads();
                    break;
                case "listsubboards":
                    _events.InvokeListSubboards();
                    break;
                case "createmessage":
                    _events.InvokePreCreateMessage();
                    if (parts.Length == 2 && Guid.TryParse(parts[1], out temp))
                    {
                        _events.InvokeCreateMessage(temp);
                    }
                    else
                    {
                        _events.InvokeCreateMessage(Guid.Empty);
                    }
                    _events.InvokePostCreateMessage();
                    break;
                case "createthread":
                    _events.InvokePreCreateThread();
                    _events.InvokeCreateThread();
                    _events.InvokePostCreateThread();
                    break;
                case "readmessage":
                    if (parts.Length == 2 && Guid.TryParse(parts[1], out temp))
                    {
                        _events.InvokeReadMessage(temp);
                        _events.InvokePostMessageRead();
                    }
                    break;
                case "deletemessage":
                    if (parts.Length == 2 && Guid.TryParse(parts[1], out temp))
                    {
                        _events.InvokePreDeleteMessage();
                        _events.InvokeDeleteMessage(temp);
                        _events.InvokePostDeleteMessage();
                    }
                    break;
                case "entermessagesection":
                    _events.InvokeEnterMessageSection();
                    break;
                case "changethread":
                    if (parts.Length == 2 && Guid.TryParse(parts[1], out temp))
                    {
                        _events.InvokeChangeThread(temp);
                    }
                    break;
                case "changesubboard":
                    if (parts.Length == 2 && Guid.TryParse(parts[1], out temp))
                    {
                        _events.InvokeEnterSubboard(temp);
                    }
                    break;
                case "listunreadmessages":
                    _events.InvokeListUnreadMessages();
                    break;
                case "readnextmessage":
                    _events.InvokeReadNextMessage();
                    break;
                case "newscan":
                    _events.InvokeNewScan();
                    break;
            }
        }
    }
}
